package com.example.cardgame

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.TextView
import android.widget.GridLayout
import androidx.core.view.children
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import cn.edu.sicnu.cardgame.CardMatchingGame
const val SCORE = "SCORE"

class GameActivity : AppCompatActivity(), View.OnClickListener {
    lateinit var game: CardMatchingGame
    val cardButton = mutableListOf<Button>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_game)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        val max_s = findViewById<TextView>(R.id.max_score)
        max_s.text = intent.getStringExtra(MAXSCORE_MSG)
        for (v in findViewById<GridLayout>(R.id.gridlayout).children) {
            if (v is Button) {
                v.setOnClickListener(this)
                cardButton.add(v)
            }
        }
        game = CardMatchingGame(cardButton.count())
        updateUI()

        val reset = findViewById<Button>(R.id.RESET)
        reset.setOnClickListener {
            game = CardMatchingGame(cardButton.count())
            updateUI()
        }
        val exit = findViewById<Button>(R.id.exit)
        exit.setOnClickListener {
            val intend = Intent(this,MainActivity::class.java)
            val text = max_s.text.toString()
            val scoreString = text.replace(Regex("[^0-9]"), "")
            val maxscore = scoreString.toInt()
            if(maxscore<game.score){
                intend.putExtra(SCORE,game.score.toString())
                setResult(3,intend)
                finish()
            }else{
                startActivity(intend);
            }
        }
    }
    fun updateUI(){
        for(button in cardButton){
            val index = cardButton.indexOf(button)
            val card = game.cardAtIndex(index)
            button.isEnabled = !card.isMatched
            if(card.isChosen){
                button.text= card.toString()
                button.setBackgroundColor(Color.WHITE);
            }else{
                button.text=""
                button.setBackgroundResource(R.drawable.background)
            }
        }
        val score = findViewById<TextView>(R.id.score)
        score.text= String.format("%s%d","得分：",game.score)
    }
    override fun onClick(v: View?){
        if(v is Button){
            val index = cardButton.indexOf(v);
            game.chooseCardAtIndex(index);
            updateUI()
        }
    }
}